package com.example.administrator.fragmentdome3;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

/**
 * Created by tangjiangxu on 3/10/17.
 */

public class LoginDialogFragment extends DialogFragment {
    private static final String TAG = "LoginDialogFragment";
    /**
     * 方法1来实现，重写onCreateView方法
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    /*@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //点击Dialog之外的地方，不能取消dialog
        getDialog().setCanceledOnTouchOutside(false);
        //这样设置Dialog不能被取消，无效。需要监听回退键来处理
        //getDialog().setCancelable(false);
        //去掉Dialog的标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_login_dialog, container);
    }*/

    /**
     * 方法2来实现，重写onCreateDialog
     * @param savedInstanceState
     * @return
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder buidler = new AlertDialog.Builder(getActivity());
        //这样设置Dialog不能被取消，无效。需要监听回退键来处理
        //buidler.setCancelable(false);
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_login_dialog, null);
        final EditText etName = (EditText) view.findViewById(R.id.et_username);
        final EditText etPwd = (EditText) view.findViewById(R.id.et_password);
        buidler.setView(view);
        buidler.setPositiveButton("登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (LoginDialogFragment.this.getTargetFragment() != null) {
                    Log.e(TAG, "LoginDialogFragment.this.getTargetFragment() != null");
                    Fragment fragment = LoginDialogFragment.this.getTargetFragment();
                    Intent intent = new Intent();
                    String name = etName.getText().toString().trim();
                    String pwd = etPwd.getText().toString().trim();
                    intent.putExtra("name", name);
                    intent.putExtra("pwd", pwd);
                    fragment.onActivityResult(ContentFragment.REQUEST_LOGIN, ContentFragment.RESULT_LOGIN, intent);
                }

            }
        });
        buidler.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        return buidler.create();
    }


}
